package com.jhj.service.bm.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.jhj.po.bm.model.Sale;
import com.jhj.po.bm.model.SaleLeave;
import com.jhj.service.bm.SaleLeaveService;
import com.jhj.service.bm.SaleService;
import com.jhj.service.impl.BaseServiceImpl;
import com.jhj.utils.DateUtil;
import com.jhj.utils.StringUtil;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class SaleLeaveServiceImpl extends BaseServiceImpl<SaleLeave> implements SaleLeaveService {
	@Autowired
	private SaleService saleService;
	@Autowired
	private SaleLeaveService saleLeaveService;
	// 当前时间date
	String getTime = DateUtil.getToday();
	// 当前小时
	int getHours = DateUtil.getHours();

	@Override
	public List<SaleLeave> selectByDefinedListPage(SaleLeave saleVo, Integer pageNo, Integer pageSize) {
		PageHelper.startPage(pageNo, pageSize);
		Example example = this.getSearchVoExample(saleVo);
		// 排序
		example.setOrderByClause("add_time desc");
		return mapper.selectByExample(example);
	}

	private Example getSearchVoExample(SaleLeave saleVo) {
		Example ex = new Example(SaleLeave.class);
		Criteria createCriteria = ex.createCriteria();
		Integer saleId = saleVo.getSaleId();
		if (saleId != null && saleId > 0) {
			createCriteria.andCondition("sale_id =" + saleId);
		}
		String saleName = saleVo.getSaleName();
		if (saleName != null && !"".equals(saleName)) {
			createCriteria.andCondition("sale_name ='" + saleName + "'");
		}
		String leaveDateStart = saleVo.getLeaveDateStart();
		if (leaveDateStart != null && !"".equals(leaveDateStart)) {
			createCriteria.andCondition("leave_date_start >='" + leaveDateStart + "'");
		}
		String leaveDateEnd = saleVo.getLeaveDateEnd();
		if (leaveDateEnd != null && !"".equals(leaveDateEnd)) {
			createCriteria.andCondition("leave_date_end <='" + leaveDateEnd + "'");
		}
		short leaveStatus = saleVo.getLeaveStatus();
		if (leaveStatus > 0) {
			createCriteria.andCondition("leave_status =" + leaveStatus);
		}
		return ex;
	}

	@Override
	public List<SaleLeave> selectBySearchVoSelf(SaleLeave saleVo, String orderBy) {
		Example example = this.getSearchVoExample(saleVo);

		// 排序
		if (!StringUtil.isEmpty(orderBy)) {
			example.setOrderByClause(orderBy);
		}

		return mapper.selectByExample(example);
	}

	@Override
	public boolean validateQian() {
		// 当前日期的请假记录
		SaleLeave leave = new SaleLeave();
		String leaveDateEnd = getTime;
		leave.setLeaveDateEnd(leaveDateEnd);
		leave.setLeaveStatus((short) 1);
		List<SaleLeave> listLeave = saleLeaveService.selectBySearchVo(leave, "");
		if (!listLeave.isEmpty()) {
			for (int i = 0; i < listLeave.size(); i++) {
				// 请假中的开始小时 结束小时
				int hourstart = listLeave.get(i).getStartTime();
				int hourend = listLeave.get(i).getEndTime();
				// 不在请假时间的小时范围内
				if (hourend < getHours) {
					return false;

				} else if (hourend > getHours) {
					SaleLeave saleLeaves = saleLeaveService.selectByPrimaryKey(listLeave.get(i).getId());
					saleLeaves.setLeaveStatus((short) 1);
					saleLeaveService.update(saleLeaves);
					// 老师请假后，改请假状态不接单
					Sale sale = saleService.selectByPrimaryKey(listLeave.get(i).getSaleId());
					sale.setStatus(2);
					saleService.update(sale);
				}
			}

			
		}
		return false;
	}

	@Override
	public boolean validateHou() {

		// 找出当前请假状态 的老师id
		Sale saleLeaves = new Sale();
		saleLeaves.setStatus(2);
		List<Sale> saleList = saleService.selectBySearchVo(saleLeaves, "");
		for (int s = 0; s < saleList.size(); s++) {
			// 相应老师请假记录(正在请假中的)
			SaleLeave saleLeave1 = new SaleLeave();
			saleLeave1.setSaleId(saleList.get(s).getSaleId());
			saleLeave1.setLeaveStatus((short) 1);
			List<SaleLeave> saleLeaveList = saleLeaveService.selectBySearchVo(saleLeave1, "");
			// 判断结束日期是否小于当前
			for (int i = 0; i < saleLeaveList.size(); i++) {
				long isOrBig = DateUtil.compareDateStr(getTime, saleLeaveList.get(i).getLeaveDateEnd());

				if (isOrBig > 0) { // 还在请假中

				} else if (isOrBig < 0) {// 请假结束
					// 老师请假后，改请假完成状态
					SaleLeave leave = saleLeaveService.selectByPrimaryKey(saleLeaveList.get(i).getId());
					leave.setLeaveStatus((short) 3);
					saleLeaveService.update(leave);
					// 老师请假后，改请假状态接单
					Sale sale = saleService.selectByPrimaryKey(saleLeaveList.get(i).getSaleId());
					sale.setStatus(1);
					saleService.update(sale);
				} else if (isOrBig == 0) {// 请假天数等于当前天 判断小时
					// 结束小时
					int endHours = saleLeaveList.get(i).getEndTime();
					if (endHours > getHours) {// 大于当前小时
						SaleLeave leave = saleLeaveService.selectByPrimaryKey(saleLeaveList.get(i).getId());
						leave.setLeaveStatus((short) 3);
						saleLeaveService.update(leave);
						// 老师请假后，改请假状态接单
						Sale sale = saleService.selectByPrimaryKey(saleLeaveList.get(i).getSaleId());
						sale.setStatus(1);
						saleService.update(sale);

					}
				}
			}
			
		}
		return false;
	}

}
